import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/model/balance.dart';
import 'package:meal/route/route.dart';
import 'package:meal/ui/scroll_refresh_config.dart';
import 'package:meal/utils/colors.dart';
import 'package:meal/utils/styles.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

///
/// 余额账单
///
class Balance extends StatefulWidget {
  final int status;

  const Balance({Key? key, required this.status}) : super(key: key);

  @override
  State<Balance> createState() => _BalanceState();
}

class _BalanceState extends State<Balance> {
  late RefreshController refreshController;

  List<BalanceDetailModel> pageList = [];

  @override
  void initState() {
    super.initState();
    refreshController = RefreshController(initialRefresh: false);
    pageList.add(BalanceDetailModel(
        payName: '微信充值', payTime: '2023-07-10', payAmount: 407.5, balance: 139.03));
    pageList.add(BalanceDetailModel(
        payName: '余额体现',
        payTime: '2023-06-10',
        payAmount: -351.5,
        balance: 127.03,
        type: BalanceTypeEnum.disburse));
    pageList.add(BalanceDetailModel(
        payName: '支付宝充值', payTime: '2023-05-27', payAmount: 217.5, balance: 120.03));
    pageList.add(BalanceDetailModel(
        payName: '互动报名',
        payTime: '2023-04-06',
        payAmount: -217.5,
        balance: 120.03,
        type: BalanceTypeEnum.disburse));
  }

  @override
  Widget build(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: refreshController,
      onRefresh: onRefresh,
      onLoading: onLoading,
      child: listViewBuilder(),
    );
  }

  Widget listViewBuilder() {
    return ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        var item = pageList[index];
        final container = Container(
          margin: const EdgeInsets.only(top: Sizes.default10),
          padding:
              const EdgeInsets.symmetric(horizontal: Sizes.default20, vertical: Sizes.default15),
          decoration: const BoxDecoration(color: Colors.white),
          child: Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(item.payName,
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.color333, fontSize: Sizes.default15)),
                  Text(item.payAmount.toString(),
                      style: StylesUtils.customTextStyle(
                          color: item.type.textColor, fontSize: Sizes.default14))
                ],
              ),
              Container(
                margin: const EdgeInsets.only(top: Sizes.default10),
                alignment: Alignment.centerLeft,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(item.payName,
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.color797, fontSize: Sizes.default12)),
                    Text('余额：${item.balance}',
                        style: StylesUtils.customTextStyle(
                            color: item.type.textColor, fontSize: Sizes.default14))
                  ],
                ),
              )
            ],
          ),
        );
        return InkWell(
          onTap: () {
            Get.toNamed(RouteGet.financeBalanceInfo, arguments: item);
          },
          child: container,
        );
      },
      itemCount: pageList.length,
      itemExtent: Sizes.default80,
    );
  }

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
    setState(() {});
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 10) {
      return refreshController.loadNoData();
    }

    /// 添加订单
    refreshController.loadComplete();
    setState(() {
      pageList.add(BalanceDetailModel(
          payName: '支付宝充值', payTime: '2023-05-27', payAmount: 217.5, balance: 120.03));
    });
  }
}
